package com.example.service;

import com.alibaba.trade.param.AlibabaAscpTradeModelProductItemInfo;
import com.alibaba.trade.param.AlibabaAscpTradeModelTradeInfo;
import com.alibaba.trade.param.AlibabaAscpTradeSkuItemDesc;
import com.alibaba.trade.param.ProducItemExtendFields;
import com.example.shiti.ProductItemInfo;
import com.example.shiti.TradeInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TradeInfoConverter {//把订单信息转化成现成的实体类

    public static TradeInfo convert(AlibabaAscpTradeModelTradeInfo source) {
        TradeInfo tradeInfo = new TradeInfo();

        // 设置基本信息
        tradeInfo.setCurrency(source.getBaseInfo().getCurrency());
        tradeInfo.setId(source.getBaseInfo().getId());
        tradeInfo.setModifyTime(source.getBaseInfo().getModifyTime());
        tradeInfo.setCreateTime(source.getBaseInfo().getCreateTime());
        tradeInfo.setPayTime(source.getBaseInfo().getPayTime());
        tradeInfo.setReceivingTime(source.getBaseInfo().getReceivingTime());
        tradeInfo.setAllDeliveredTime(source.getBaseInfo().getAllDeliveredTime());
        tradeInfo.setCompleteTime(source.getBaseInfo().getCompleteTime());

        // 设置买家联系人信息
        tradeInfo.setBuyerContactName(source.getBaseInfo().getBuyerContact().getName());
        tradeInfo.setBuyerContactMobile(source.getBaseInfo().getBuyerContact().getMobile());
        tradeInfo.setBuyerContactPhone(source.getBaseInfo().getBuyerContact().getPhone());
        tradeInfo.setBuyerContactProvince(source.getBaseInfo().getBuyerContact().getProvince());
        tradeInfo.setBuyerContactCity(source.getBaseInfo().getBuyerContact().getCity());
        tradeInfo.setBuyerContactArea(source.getBaseInfo().getBuyerContact().getArea());
        tradeInfo.setBuyerContactTown(source.getBaseInfo().getBuyerContact().getTown());
        tradeInfo.setBuyerContactAddress(source.getBaseInfo().getBuyerContact().getAddress());
        tradeInfo.setBuyerContactZip(source.getBaseInfo().getBuyerContact().getZip());
        tradeInfo.setCaid(source.getBaseInfo().getBuyerContact().getCaid());
        tradeInfo.setOaid(source.getBaseInfo().getBuyerContact().getOaid());

        // 设置交易状态和退款状态
        tradeInfo.setStatus(source.getBaseInfo().getStatus());
        tradeInfo.setRefundStatus(source.getBaseInfo().getRefundStatus());
        tradeInfo.setRemark(source.getBaseInfo().getRemark());

        // 设置供应商信息
        tradeInfo.setAscpSupplierId(source.getBaseInfo().getAscpSupplierId());
        tradeInfo.setAscpSupplierName(source.getBaseInfo().getAscpSupplierName());

        // 设置金额信息
        tradeInfo.setShippingFee(source.getBaseInfo().getShippingFee());
        tradeInfo.setTotalAmount(source.getBaseInfo().getTotalAmount());

        // 设置商品条目信息
        List<ProductItemInfo> productItems = new ArrayList<>();
        for (AlibabaAscpTradeModelProductItemInfo item : source.getProductItems()) {
            ProductItemInfo productItemInfo = new ProductItemInfo();
            productItemInfo.setSubItemID(item.getSubItemID());
            productItemInfo.setProductID(item.getProductID());
            productItemInfo.setSkuID(item.getSkuID());
            productItemInfo.setName(item.getName());
            productItemInfo.setQuantity(item.getQuantity());
            productItemInfo.setProductImgUrl(Arrays.toString(item.getProductImgUrl()));
            productItemInfo.setStatus(item.getStatus());
            productItemInfo.setRefundId(item.getRefundId());
            productItemInfo.setRefundStatus(item.getRefundStatus());
            productItemInfo.setItemAmount(item.getItemAmount());
            productItemInfo.setCargoNumber(item.getCargoNumber());
            productItemInfo.setProductCargoNumber(item.getProductCargoNumber());
            productItemInfo.setSkuInfosName("");
            productItemInfo.setSkuInfosValue("");
            productItemInfo.setTradeInfoId(tradeInfo.getId());
            for (AlibabaAscpTradeSkuItemDesc sku : item.getSkuInfos()) {
                productItemInfo.setSkuInfosName(productItemInfo.getSkuInfosName()+","+sku.getName());
                productItemInfo.setSkuInfosName(productItemInfo.getSkuInfosValue()+","+sku.getValue());
            }
            productItemInfo.setProductItemExtendFieldsKey("");
            productItemInfo.setProductItemExtendFieldsValue("");
            if(item.getProductItemExtendsFileds()!=null){
                for (ProducItemExtendFields field : item.getProductItemExtendsFileds()) {
                    productItemInfo.setProductItemExtendFieldsKey(productItemInfo.getProductItemExtendFieldsKey()+","+field.getKey());
                    productItemInfo.setProductItemExtendFieldsValue(productItemInfo.getProductItemExtendFieldsValue()+","+field.getValue());
                }
            }
            productItems.add(productItemInfo);
        }
        tradeInfo.setProductItems(productItems);
        return tradeInfo;
        }
}

